package com.copera.cn.kaka;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.copera.cn.kaka.adapter.ShopUserAdapter;
import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.HttpUtils;
import com.copera.cn.kaka.util.AsyncImageLoader.ImageCallBack;
import com.copera.cn.kaka.vo.ShopUserVo;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class ShopUserActivity<dialog> extends BaseActivity {
	
	private EditText editText1,editText2;
	/*private ApplicationCustomer appc;*/
	private RelativeLayout back;
	private Button loginButton;
	private Handler handler;
	private ListView lv;
	private String userId = null;
	private ProgressDialog dialog;
	private ShopUserAdapter shopUserAdapter;
	/*private ListView listView;*/
	/*List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();*/
	List<ShopUserVo> list = new ArrayList<ShopUserVo>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		refreshView(getLayoutInflater().inflate(R.layout.s_user, null));
		/*appc = (ApplicationCustomer) getApplication();*/
		initViews();
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {

				case ActionUrls.FAILURE:
					Toast.makeText(ShopUserActivity.this, (String) msg.obj,
							Toast.LENGTH_SHORT).show();
					break;
				case 12:
					userId = msg.obj.toString();
					dialogConfirm();
				}
			}
		};		
		shopUserAdapter = new ShopUserAdapter(list, this,getAppCustomer(), handler);
		
		lv.setAdapter(shopUserAdapter);
		
		
		initEvent();
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	} 
	
	@Override
	protected void onResume() {
		findUser();
		super.onResume();
	}

	private void delete() {
		new Thread() {
			public void run() {
				try {
					String json = HttpUtils.get(ActionUrls.URL_DELETEUSER + "?userId=" + userId + "&shopId=" + getAppCustomer().getShop().getId(), getAppCustomer().getHttpClient());
					JSONObject jo = new JSONObject(json);
					String message = jo.getString("message");
					if(message.equals("删除成功")){
						Intent intent = new Intent();
						intent.setClass(ShopUserActivity.this, ShopUserActivity.class);
						startActivity(intent);
						finish();
					}else{
						Message msg = handler.obtainMessage(ActionUrls.FAILURE, message);
						handler.sendMessage(msg);
					}
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (JSONException e) {
					e.printStackTrace();
				}
			};
		}.start();				
	}
	private void dialogConfirm() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				ShopUserActivity.this);
		builder.setTitle("删除");
		builder.setMessage("确定要删除吗？");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				delete();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.create().show();
	}
	
	private void initViews(){
		back = (RelativeLayout) findViewById(R.id.back);	
		editText1 = (EditText) findViewById(R.id.editText1);
		editText2 = (EditText) findViewById(R.id.editText2);
		loginButton = (Button) findViewById(R.id.loginButton);
		lv = (ListView)findViewById(R.id.userList);
		dialog=getpDialog(this);
	}

	private void initEvent(){
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(isVilid()){
				new Thread() {
					public void run() {
						try {
							String json = HttpUtils.get(ActionUrls.URL_SHOPUSER_SAVE+"?id="+getAppCustomer().getShop().getId()+"&userName="+editText1.getText()+"&userPass="+editText2.getText(), getAppCustomer().getHttpClient());
							JSONObject jo = new JSONObject(json);
							String message = jo.getString("message");
							if(message.equals("保存成功")){
								Intent intent = new Intent();
								intent.setClass(ShopUserActivity.this, ShopUserActivity.class);
								startActivity(intent);
								finish();
							}else{
								Message msg = handler.obtainMessage(ActionUrls.FAILURE, message);
								handler.sendMessage(msg);
							}
						} catch (ClientProtocolException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					};
				}.start();				
				}	
			}
		});	
	}
	public void findUser() {

		new AsyncTask<String, Integer, JSONObject>() {

			@Override
			protected JSONObject doInBackground(String... params) {
				JSONObject jsonObj = null;
				try {
					String jsonStr = HttpUtils.get(params[0], getAppCustomer()
							.getHttpClient());

					if (jsonStr != null) {
						jsonObj = new JSONObject(jsonStr);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return jsonObj;
			}

			@Override
			protected void onPostExecute(JSONObject result) {

				if (result != null) {
					try {
						JSONArray jsonArr = result.getJSONArray("list");
						if (jsonArr != null && jsonArr.length() > 0) {
							for (int i = 0; i < jsonArr.length(); i++) {
								JSONObject object = jsonArr.getJSONObject(i);
								ShopUserVo vo = new ShopUserVo();
								vo.setPhone(object.getString("loginName"));
								vo.setPassword(object.getString("password"));
								vo.setId(object.getString("id"));
								list.add(vo);
								shopUserAdapter.notifyDataSetChanged();
							}
						}

					} catch (Exception e) {
						e.printStackTrace();
						if (e.getMessage() != null) {

						}
					}
				}
			}

		}.execute(ActionUrls.URL_SHOPUSER_LIST+"?id="+getAppCustomer().getShop().getId());

	}
	
	private boolean isVilid(){
		if (editText1.getText().toString() == null || "".equals(editText1.getText().toString().trim())) {
			Toast.makeText(ShopUserActivity.this, "账号不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (editText2.getText().toString() == null || "".equals(editText2.getText().toString().trim())) {
			Toast.makeText(ShopUserActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

}
